package com.gozap.socket.think;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class JabberServer {

	public static final int PORT = 46378;

	public static void main(String[] args) throws IOException {

		ServerSocket s = new ServerSocket(PORT);

		System.out.println("Started: " + s);

		try {
			Socket socket = s.accept();
			try {
				System.out.println("Connection accepted: " + socket);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));

				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);

				// while (true) {
				String str = in.readLine();
				// if (null == str || "END".equals(str))
				// break;
				System.out.println("Echoing: " + str);
				out.println("the end of hello world");
				// }
				// Always close the two sockets...
			} finally {
				System.out.println("socket closing...");
				socket.close();
			}

		} finally {
			System.out.println("server socket closing...");
			s.close();
		}
	}

}
